package org.vectrics.cms;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.domain.Domain;

public class UploadImageHelper {
	private transient Logger log = Logger.getLogger(this.getClass()); 

	public UploadImageHelper() {
		super();
	}


	public void execute(InputStream uploadStream, Domain domain, 
			String section, String code) {
		
		ContentHelper contentHelper = new ContentHelper();
		String filePath = contentHelper.getContentFileName(domain, section, code);
		try {
			Date now = new Date();
			FileOutputStream fileStream = new FileOutputStream(filePath);

			boolean done = false;
			while (!done) {
				int c = uploadStream.read();
				if (c < 0)
					done = true;
				else {
					fileStream.write(c);
				}
			}
			fileStream.close();
		}
		catch (Exception ex) {
			throw new SystemException("Exception building report.  File name: "
					+ filePath, ex);
		}
	}
	
}
